<template>
  <div>
    <router-link
      tab="div"
      to="/"
      class="header-abs"
      v-show="showAbs">
      <span class="iconfont header-abs-back">&#xe624;</span>
    </router-link>
    <div
      class="header-fixed"
      v-show="!showAbs"
      :style="opacityStyle">
      <h1>八达岭长城</h1>
      <router-link to="/">
        <span class="iconfont header-fixed-icon">&#xe624;</span>
      </router-link>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showAbs: true,
      opacityStyle: {
        opacity: 0
      }
    }
  },
  methods: {
    handleScroll () {
      let top = document.documentElement.scrollTop
      let opacity = top / 140
      this.opacity = opacity > 1 ? 1 : opacity
      this.opacityStyle = {opacity}
      if (top > 0) {
        this.showAbs = false
      } else {
        this.showAbs = true
      }
    }
  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll)
  },
  unmonted () {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/variable.styl'
  .header-abs
    width: .8rem
    height: .8rem
    line-height: .8rem
    border-radius: .4rem
    background-color: #000
    color: #fff
    opacity: .5
    text-align: center
    position: absolute
    top: .2rem
    left: .2rem
    .header-abs-back
      font-size: .4rem
      font-weight: bold
  .header-fixed
    height: .7rem
    line-height: .7rem
    background: $bgColor
    color: #fff
    font-size: .32rem
    text-align: center
    position: fixed
    top: 0
    left: 0
    right: 0
    z-index: 2
    .header-fixed-icon
      width: .8rem
      position: absolute
      left: 0
      top: -.05rem
      font-weight: bold
      font-size: .34rem
      color: #fff
</style>
